//
//  VisitorLoginView.swift
//  SinaWeibo
//
//  Created by Arvin on 15/11/8.
//  Copyright © 2015年 Arvin. All rights reserved.
//

import UIKit

// 协议
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    func userWillLogin() // 必须实现方法
    optional func userWillRegister() // 可选实现的方法
}

class VisitorLoginView: UIView {
    //MARK; 代理
    weak var visitorDelegat: VisitorLoginViewDelegate?
    
    //MARK: 外部调用方法
    func setTextAndIconView(text: String, icon: String?) {
        tipLabel.text = text
        if icon == nil {
            startAnimation()
        } else {
            circleView.hidden = true
            iconView.image = UIImage(named: icon!)
        }
    }
    
    private func startAnimation() {
        let anim = CABasicAnimation()
        anim.keyPath = "transform.rotation"
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.toValue = 2 * M_PI
        // 当动画结束, 或者图层处于非活跃状态, 动画不移除突层
        anim.removedOnCompletion  = false
        circleView.layer.addAnimation(anim, forKey: nil);
    }
    
    //MARK: 按钮的点击
    // userWillRegister后面加 ? 相当于OC的response的
    @objc func registerBtnDidClick() { // 按钮点击调用的方法
        visitorDelegat?.userWillRegister?() // 代理调用的方法
    }
    
    @objc func loginBtnDidClick() {
        visitorDelegat?.userWillLogin()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubUi()
    }
    
    required init?(coder aDecoder: NSCoder) {
        // fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        addSubUi()
    }
    
    
    //MARK: 添加UI
    private func addSubUi() {
        
        backgroundColor = UIColor(white: 0.91, alpha: 1.0)
        
        addSubview(circleView)
        addSubview(backView)
        addSubview(iconView)
        addSubview(tipLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        // 设置子控件的autoresizing失效
        for subview in subviews {
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        // 设置圆的约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -10))
        // 设置icon的约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        // 设置tip的约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 5))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 46))
        
        // 设置login的约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        // 设置reg的约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tipLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: loginBtn, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: loginBtn, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
        // 设置masks的约束 注意这里是addConstraints VS 上面是addConstraint
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-40)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView, "registerBtn":registerBtn]))
        
    }
    
    //MARK: 懒加载控件
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var tipLabel: UILabel = {
        let tip = UILabel()
        tip.text = "关注一些人，回这里看看有什么,惊喜关注一些人，回这里看看有什么惊喜"
        tip.textAlignment = NSTextAlignment.Left
        tip.numberOfLines = 0
        tip.font = UIFont.systemFontOfSize(13)
        return tip
        }()
    
    private lazy var registerBtn: UIButton = {
        let reg = UIButton(title: "注册", imageN: "common_button_white_disable", imageH: nil, color: UIColor.orangeColor())
        reg.addTarget(self, action: "registerBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        return reg
        }()
    
    private lazy var loginBtn: UIButton = {
        let log = UIButton(title: "登录", imageN: "common_button_white_disable", imageH: nil, color: UIColor.blackColor())
        log.addTarget(self, action: "loginBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        return log
        }()
}
